package com.muye.dianli.activity;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;

import org.apache.http.entity.mime.content.StringBody;

import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.muye.constant.C;
import com.muye.constant.U;
import com.muye.dianli.BaseActivity;
import com.muye.dianli.R;
import com.zvezda.algorithm.utils.MD5;
import com.zvezda.http.utils.HttpConst;
import com.zvezda.http.utils.KeyValue;
import com.zvezda.http.utils.RequestHttpListener;
import com.zvezda.http.utils.RequestHttpPostThread;

/**
 * 文章详情页面
 * 
 * @author TangChao
 *
 */
public class ArticleDetailActivity extends BaseActivity implements OnClickListener, OnKeyListener
{
	/**
	 * 请求数据线程
	 */
	private RequestHttpPostThread requestHttpPostThread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.article_detail_layout);
		createTitle(getIntent().getStringExtra("TITLE_NAME"));
		getArticleDetail("0", getIntent().getStringExtra("ID"), "1", "10", "");
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
	
	/**
	 * 初始化标题栏
	 */
	private void createTitle(String str)
	{
		TextView titleName = (TextView) findViewById(R.id.title_bar_name_text_view);
		titleName.setText(str);
		ImageView titleLeftBtn = (ImageView) findViewById(R.id.title_bar_left_image_view);
		titleLeftBtn.setVisibility(View.VISIBLE);
		titleLeftBtn.setImageResource(R.drawable.title_back_icon);
		titleLeftBtn.setOnClickListener(this);
	}
	
	/**
	 * 请求文章详情
	 */
	private void getArticleDetail(String type, String cid, String page, String size, String search)
	{
		C.openPragressDialog(this, this, R.string.in_request_data);
		ArrayList<KeyValue<String, Object>> paramsList = new ArrayList<KeyValue<String, Object>>();
		String str = C.APPID + cid + page + search + size + type + C.CONSTANT;
		try
		{
			paramsList.add(new KeyValue<String, Object>("appId", new StringBody(C.APPID, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("cid", new StringBody(cid, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("page", new StringBody(page, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("search", new StringBody(search, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("size", new StringBody(size, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("type", new StringBody(type, Charset.forName("utf-8"))));
			paramsList
					.add(new KeyValue<String, Object>("sign", new StringBody(MD5.getMD5_32(str), Charset.forName("utf-8"))));
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		requestHttpPostThread = new RequestHttpPostThread(getArticleDetailListener, U.LIST, paramsList);
		requestHttpPostThread.start();
	}
	
	/**
	 * 我请求文章详情返回监听
	 */
	private RequestHttpListener getArticleDetailListener = new RequestHttpListener()
	{
		
		@Override
		public void requestHttp(Message msg)
		{
			Bundle bundle = msg.getData();
			int result = bundle.getInt(HttpConst.ACTION_RESULT, HttpConst.ACTION_FAIL);
			if (result == HttpConst.ACTION_SUCCESS)
			{
				byte[] dataArr = bundle.getByteArray(HttpConst.DATA_ARR);
				try
				{
					String str = new String(dataArr, "utf-8");
					Log.i(C.TAG, "请求文章详情结果----->" + str);
				}
				catch (UnsupportedEncodingException e)
				{
					e.printStackTrace();
				}
			}
		}
	};
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.title_bar_left_image_view:
				finish();
				break;
			
			default:
				break;
		}
	}
	
	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
	{
		if (requestHttpPostThread != null)
		{
			requestHttpPostThread.interrupt();
			requestHttpPostThread.setRequestHttpListener(null);
		}
		return false;
	}
}
